package com.example.server.entity;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.StringUtils;

import java.util.Collection;

/**
 * @Author: xieyonghui
 * @Date: 2019/7/4
 * @Description:
 */
@Data
public class JwtUserDetails extends SysUser implements UserDetails{
    private static final long serialVersionUID = 1L;
    private final Collection<? extends GrantedAuthority> authorities;
    /** 账户未过期 */
    private final boolean accountNonExpired;
    /** 账户未锁定 */
    private final boolean accountNonLocked;
    /** 认证未过期 */
    private final boolean credentialsNonExpired;
    /** 账户是否可用 */
    private final boolean enabled;

    public JwtUserDetails(SysUser user, boolean credentialsNonExpired, Collection<? extends GrantedAuthority> authorities) {
        this(user, true, credentialsNonExpired, true, authorities);
    }

    public JwtUserDetails(SysUser user, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
        if (!StringUtils.isEmpty(user.getUsername())) {
            this.setId(user.getId());
            this.setUsername(user.getUsername());
            this.setPassword(user.getPassword());
            this.enabled = user.getEnabled();
            this.accountNonExpired = accountNonExpired;
            this.credentialsNonExpired = credentialsNonExpired;
            this.accountNonLocked = accountNonLocked;
            this.authorities = authorities;
        } else {
            throw new IllegalArgumentException("Cannot pass null or empty values to constructor");
        }
    }

    public JwtUserDetails(Long userId, String username, boolean enabled, boolean credentialsNonExpired, Collection<? extends GrantedAuthority> authorities) {
        if (!StringUtils.isEmpty(username)) {
            this.setUsername(username);
            this.setId(userId);
            this.enabled = enabled;
            this.accountNonExpired = true;
            this.credentialsNonExpired = credentialsNonExpired;
            this.accountNonLocked = true;
            this.authorities = authorities;
        } else {
            throw new IllegalArgumentException("Cannot pass null or empty values to constructor");
        }
    }

    /**
     * 添加用户拥有的权限和角色
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    /**
     * 账户是否过期
     * @return
     */
    @Override
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }

    /**
     * 是否禁用
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    /**
     * 密码是否过期
     * @return
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    /**
     * 是否启用
     * @return
     */
    @Override
    public boolean isEnabled() {
        return enabled;
    }

}
